{template 'public/header'}
<ul class="nav nav-tabs">
	<li {if $do == 'store'}class="active"{/if}><a href="{php echo $this->createWebUrl('ptcategory');}">添加驿站商品分类</a></li>
	<li {if $do == 'ptgoods'}class="active"{/if}><a href="{php echo $this->createWebUrl('ptgoods');}">驿站商品列表</a></li>
</ul>
{if $op == 'post'}
<form class="form-horizontal form" id="form1" action="" method="post" enctype="multipart/form-data">
	<input type="hidden" name="sid" value="{$sid}">
	<div class="main">
		<div class="panel panel-default">
			<div class="panel-heading">添加驿站商品</div>
			<div class="panel-body">
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品名称</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" class="form-control" name="title" value="{$item['title']}">
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品简述</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" class="form-control" name="intro" value="{$item['intro']}">
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品分类</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<select name="cid" id="cid" class="form-control">
							{loop $category $li}
								<option value="{$li['id']}" {if $item['cid'] == $li['id'] || $_GPC['cid'] == $li['id']}selected{/if}>{$li['title']}</option>
							{/loop}
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品图片</label>
					<div class="col-sm-9 col-xs-12">
						{php echo tpl_form_field_image('thumb', $item['thumb']);}
						<div class="help-block">图片需为正方形, 推荐大小: 500*500</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品原价</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" class="form-control" name="price" value="{$item['price']}">
						<div class="help-block"></div>
					</div>
				</div>
				<div class="form-group hide">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>优惠价</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" class="form-control" name="discount_price" value="{$item['discount_price']}">
						<div class="help-block">如果该商品不优惠,则留空.</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require"> 启用规格</span></label>
					<div class="col-sm-9 col-xs-12">
						<label class="checkbox-inline"><input type="checkbox" name="is_options" value="1" {if $item['is_options'] == 1}checked{/if} onclick="$('#options').toggle();"> 启用</label>
						<br>
						<div id="options" {if $item['is_options']}style="display:block"{/if}>
							<div class="tpl">
								{if !empty($item['options'])}
									{loop $item['options'] $row}
										<div class="input-group">
											<input type="hidden" name="options[id][]" value="{$row['id']}">
											<span class="input-group-addon">规格名称</span>
											<input type="text" name="options[name][]" value="{$row['name']}" class="form-control" placeholder="规格名称">
											<span class="input-group-addon">价格</span>
											<input type="text" name="options[price][]" value="{$row['price']}" class="form-control" placeholder="价格">
											<span class="input-group-addon">库存</span>
											<input type="text" name="options[total][]" value="{$row['total']}" class="form-control" placeholder="库存">
											<span class="input-group-addon">排序</span>
											<input type="text" name="options[displayorder][]" value="{$row['displayorder']}" class="form-control" placeholder="排序">
											<span class="input-group-addon"><a href="javascript:;" class="delOptions"><i class="fa fa-times"></i></a></span>
										</div>
									{/loop}
								{else}
									<div class="input-group">
										<input type="hidden" name="options[id][]" value="">
										<span class="input-group-addon">规格名称</span>
										<input type="text" name="options[name][]" class="form-control" placeholder="规格名称">
										<span class="input-group-addon">价格</span>
										<input type="text" name="options[price][]" class="form-control" placeholder="价格">
										<span class="input-group-addon">库存</span>
										<input type="text" name="options[total][]" class="form-control" placeholder="库存" vlaue="-1">
										<span class="input-group-addon">排序</span>
										<input type="text" name="options[displayorder][]" class="form-control" placeholder="排序" value="0">
										<span class="input-group-addon"><a href="javascript:;" class="delOptions"><i class="fa fa-times"></i></a></span>
									</div>
								{/if}
							</div>
							<a href="javascript:;" class="addOptions"><i class="fa fa-plus-circle"></i> 添加规格</a>
							<span class="help-block">最多可添加5个规格. -1为库存无限制. 排序数字越大越靠前</span>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品单位</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" class="form-control" name="unitname" value="{$item['unitname']}">
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>自定义标签</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<input type="text" class="form-control" name="label" value="{$item['label']}">
						<div class="help-block">可设置为：热卖，新品，爆款等。只能设置一个，不超过两个字</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>总库存</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<input type="text" class="form-control" name="total" value="{$item['total']}">
						<div class="help-block">-1为库存无限制</div>
					</div>	
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>已卖出</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<input type="text" class="form-control" name="sailed" value="{$item['sailed']}">
						<div class="help-block">已卖出的份数默认会根据订单自动更新。您也可以手动设置</div>
					</div>	
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>是否上架</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<label class="radio-inline"><input type="radio" name="status" value="1" {if $item['status'] == 1 || !$item['status']}checked{/if}> 上架</label>
						<label class="radio-inline"><input type="radio" name="status" value="2" {if $item['status'] == 2}checked{/if}> 下架</label>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>设置为热销</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<label class="radio-inline"><input type="radio" name="is_hot" value="1" {if $item['is_hot'] == 1}checked{/if}> 设置</label>
						<label class="radio-inline"><input type="radio" name="is_hot" value="0" {if !$item['is_hot']}checked{/if}> 不设置</label>
						<div class="help-block">设为热销后,将在门店信息列表页显示</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">排序</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" class="form-control" name="displayorder" value="{$item['displayorder']}">
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">商品详情</label>
					<div class="col-sm-9 col-xs-12">
						{php echo itpl_ueditor('description', $item['description']);}
					</div>
				</div>
			</div>
		</div>
		<div class="form-group">
			<div class="col-sm-9 col-xs-9 col-md-9">
				<input type="hidden" name="token" value="{$_W['token']}">
				<input name="submit" id="submit" type="submit" value="提交" class="btn btn-primary col-lg-1">
			</div>	
		</div>
	</div>
</form>
<script type="text/javascript">
$(function(){
	$('#form1').submit(function(){
		if($.trim($(':text[name="title"]').val()) == '') {
			util.message('请填写商品名称');
			return false;
		}
		if($.trim($(':text[name="intro"]').val()) == '') {
			util.message('请填写商品简述');
			return false;
		}
		if(!$('#cid').val()) {
			util.message("请选择商品分类");
			return false;
		}
		var price = parseInt($.trim($(':text[name="price"]').val()));
		if(isNaN(price)) {
			util.message("商品原价不能为空");
			return false;
		}

		//启用规格
		var is_options = $(':checkbox[name="is_options"]').prop('checked');
		if(is_options) {
			var name_flag = price_flag = false;
			$.each($(':text[name="options[name][]"]'), function(){
				if(!$.trim($(this).val())) {
					name_flag = true;
				}
				if(!$.trim($(this).next().next().val())) {
					price_flag = true;
				}
			});
			if(name_flag) {
				util.message('存在没有设置名称的规格项');
				return false;
			}
			if(price_flag) {
				util.message('存在没有设置价格的规格项');
				return false;
			}
		}

		if($.trim($(':text[name="thumb"]').val()) == '') {
			util.message('请添加商品图片');
			return false;
		}
		if($.trim($(':text[name="unitname"]').val()) == '') {
			util.message('请填写商品单位');
			return false;
		}
		var total = parseInt($.trim($(':text[name="total"]').val()));
		if(isNaN(total)) {
			util.message("总库存必须为整数");
			return false;
		}
	});
});

$('.addOptions').click(function(){
	var html = '<div class="input-group">'+
					'<input type="hidden" name="options[id][]" value="">'+
					'<span class="input-group-addon">规格名称</span>'+
					'<input type="text" name="options[name][]" class="form-control" placeholder="规格名称">'+
					'<span class="input-group-addon">价格</span>'+
					'<input type="text" name="options[price][]" class="form-control" placeholder="价格">'+
					'<span class="input-group-addon">库存</span>'+
					'<input type="text" name="options[total][]" class="form-control" value="-1" placeholder="库存">'+
					'<span class="input-group-addon">排序</span>'+
					'<input type="text" name="options[displayorder][]" class="form-control" value="0" placeholder="排序">'+
					'<span class="input-group-addon"><a href="javascript:;" class="" onclick="$(this).parent().parent().remove();"><i class="fa fa-times"></i></a></span>'+
				'</div>';
	$('#options .tpl').append(html);
});

$('.delOptions').click(function(){
	$(this).parent().parent().remove();
});
</script>
{elseif $op == 'list'}
<div class="main">
	<div class="panel panel-info">
		<div class="panel-heading">筛选</div>
		<div class="panel-body">
			<form action="./index.php" method="get" class="form-horizontal" role="form">
				<input type="hidden" name="c" value="site">
				<input type="hidden" name="a" value="entry">
				<input type="hidden" name="m" value="we7_wmall">
				<input type="hidden" name="do" value="goods"/>
				<input type="hidden" name="op" value="list"/>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="require">* </span>商品分类</label>
					<div class="col-sm-9 col-xs-9 col-md-9">
						<select name="cid" id="cid" class="form-control">
							<option value="">不限</option>
							{loop $category $li}
								<option value="{$li['id']}" {if $li['id'] == $_GPC['cid']}selected{/if}>{$li['title']}</option>
							{/loop}
						</select>
					</div>	
				</div>
				<div class="form-group clearfix">
					<label class="col-xs-12 col-sm-2 col-md-2 control-label">商品名称</label>
					<div class="col-sm-7 col-lg-8 col-md-8 col-xs-12">
						<input class="form-control" name="keyword" id="" type="text" value="{$_GPC['keyword']}">
					</div>
					<div class="col-xs-12 col-sm-3 col-md-2 col-lg-1">
						<button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div class="panel panel-default">
		<div class="panel-body">
			<a class="btn btn-primary" href="{php echo $this->createWebUrl('ptgoods', array('op' => 'post'));}"/><i class="fa fa-plus-circle"> </i> 添加商品</a>
		</div>
	</div>
	

	<form class="form-horizontal" action="{php echo $this->createWebUrl('ptgoods', array('op' => 'batch_del'));}" method="post" id="form-goods">
		<div class="panel panel-default">
			<div class="panel-body table-responsive">
				<table class="table table-hover">
					<thead class="navbar-inner">
						<tr>
							<th width="30"></th>
							<th>缩略图</th>
							<th>商品名称</th>
							<th>所属分类</th>
							<th>价格</th>
							<th>总库存</th>
							<th>已售出</th>
							<th>标签</th>
							<th>是否上架</th>
							<th style="width:150px; text-align:right;">操作</th>
						</tr>
					</thead>
					<tbody>
						{loop $lists $item}
						<tr>
							<td><input type="checkbox" name="id[]" value="{$item['id']}"></td>
							<td><img src="{php echo tomedia($item['thumb']);}" width="38" style="border-radius: 3px;"></td>
							<td>{$item['title']}</td>
							<td>{$category[$item['cid']]['title']}</td>
							<td>
								{$item['price']}元
							</td>
							<td>{if $item['total'] == -1}无限库存{else}{$item['total']}份{/if}</td>
							<td>{$item['sailed']} 份</td>
							<td>{$item['label']}</td>
							<td>
								<input type="checkbox" value="{$item['status']}" name="status[]" data-id="{$item['id']}" {if $item['status'] == 1}checked{/if}>
							</td>
							<td style="text-align:right;">
								<a href="{php echo $this->createWebUrl('ptgoods', array('op' => 'post', 'id' => $item['id']))}" class="btn btn-default btn-sm" title="编辑" data-toggle="tooltip" data-placement="top"><i class="fa fa-edit"> </i></a>
								<a href="{php echo $this->createWebUrl('ptgoods', array('op' => 'del', 'id' => $item['id']))}" class="btn btn-default btn-sm" title="删除" data-toggle="tooltip" data-placement="top" onclick="if(!confirm('删除后将不可恢复，确定删除吗?')) return false;"><i class="fa fa-times"> </i></a>
								<a href="{php echo $this->createWebUrl('ptgoods', array('op' => 'copy', 'id' => $item['id']))}" class="btn btn-default btn-sm" title="复制" data-toggle="tooltip" data-placement="top" onclick="if(!confirm('确定复制吗?')) return false;">复制</a>
							</td>
						</tr>
						{/loop}
						<tr>
							<td><input type="checkbox" id="selectall"></td>
							<td colspan="9">
								<input type="hidden" name="token" value="{$_W['token']}">
								<input type="submit" name="submit" value="批量删除" class="btn btn-primary">
								<a href="{php echo $this->createWebUrl('ptgoods', array('op' => 'batch_up'));}"><input type="button" id="up" value="同步到所有门店" class="btn btn-primary"></a>
								<input type="button" id="down" value="下架同步到所有" class="btn btn-primary">
							</td>
						</tr>
						
					</tbody>
				</table>
			</div>
		</div>
		{$pager}
	</form>
</div>
<script type="text/javascript">
require(['bootstrap.switch'], function($){
	$('.form-file').submit(function(){
		if(!$(':file[name="file"]').val()) {
			util.message('请先上传要导入的文件', '', 'error');
			return false;
		}
	});

	$(':checkbox[name="status[]"]').bootstrapSwitch();
	$(':checkbox[name="status[]"]').on('switchChange.bootstrapSwitch', function(e, state){
		var status = this.checked ? 1 : 0;
		var id = $(this).data('id');
		$.post("{php echo $this->createWebUrl('ptgoods', array('op' => 'status'))}", {status: status, id: id}, function(resp){
			setTimeout(function(){
				location.reload();
			}, 500)
		});
	});

	$('#selectall').click(function(){
		$('#form-goods :checkbox').prop('checked', $(this).prop('checked'));
	});

	$('#form-goods').submit(function(){
		if($('#form-goods :checkbox[name="id[]"]:checked').length == 0) {
			util.message('请选择要删除的商品', '', 'error');
			return false;
		}
		if(!confirm('确定删除吗')) {
			return false;
		}
		return true;
	});


//同步所有
	$('#up').click(function(){
		if($('#form-goods :checkbox[name="id[]"]:checked').length == 0) {
			util.message('请选择要同步的商品', '', 'error');
			return false;
		}
		if(!confirm('确定同步吗')) {
			return false;
		}else{
			var ptgid='';
			$($('#form-goods :checkbox[name="id[]"]:checked')).each(function(){
　　			ptgid+=this.value + ',';    //遍历被选中CheckBox元素的集合 得到Value值
			});
				$.get("{php echo $this->createWebUrl('ptgoods', array('op' => 'batch_up'));}",{ptgid:ptgid},function(result){
					  var result = $.parseJSON(result);
					if(result.message.errno == -1) {
						util.message(result.message.message);
						return false;
					}
					util.message('同步到所有驿站成功', "{php echo $this->createWebUrl('ptgoods', array('op' => 'list'))}", 'success');
				})
			
		}
	return false;
	});



//取消所有同步
	$('#down').click(function(){
		if($('#form-goods :checkbox[name="id[]"]:checked').length == 0) {
			util.message('请选择要下架同步的商品', '', 'error');
			return false;
		}
		if(!confirm('确定下架同步吗')) {
			return false;
		}else{
			var ptgid='';
			$($('#form-goods :checkbox[name="id[]"]:checked')).each(function(){
　　			ptgid+=this.value + ',';    //遍历被选中CheckBox元素的集合 得到Value值
			});
			$.get("{php echo $this->createWebUrl('ptgoods', array('op' => 'batch_down'));}",{ptgid:ptgid},function(result){
				  var result = $.parseJSON(result);
					if(result.message.errno == -1) {
						util.message(result.message.message);
						return false;
					}
					util.message('下架同步到所有驿站成功', "{php echo $this->createWebUrl('ptgoods', array('op' => 'list'))}", 'success');
			})
		}
	return false;
	});

});
</script>
{/if}







{template 'common'}
{template 'public/footer'}